package com.zhyl.zhyl_detect_demo.widget;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.zhyl.detectibrary.util.DetectItem;
import com.zhyl.zhyl_detect_demo.R;

import java.util.LinkedList;
import java.util.List;

/**
 * 显示检测框组件
 */
public class ObjectReactView extends View {

    private List<DetectItem> results = new LinkedList<>();
    private Paint boxPaint;
    private Paint textBackgroundPaint;
    private Paint textPaint;
    private float scaleFactor;
    private Rect bounds = new Rect();

    public ObjectReactView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaints();
    }

    // 初始化画笔
    private void initPaints() {
        textBackgroundPaint = new Paint();
        textBackgroundPaint.setColor(Color.BLACK);
        textBackgroundPaint.setStyle(Paint.Style.FILL);
        textBackgroundPaint.setTextSize(50);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(50);

        boxPaint = new Paint();
        boxPaint.setColor(ContextCompat.getColor(getContext(), R.color.purple_200));
        boxPaint.setStrokeWidth(4);
        boxPaint.setStyle(Paint.Style.STROKE);
    }

    // 重写绘制方法
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (DetectItem result : results) {
            RectF boundingBox = result.getBoundingBox();

            float top = boundingBox.top * scaleFactor;
            float bottom = boundingBox.bottom * scaleFactor;
            float left = boundingBox.left * scaleFactor;
            float right = boundingBox.right * scaleFactor;

            // 绘制检测对象周围的边界框
            RectF drawableRect = new RectF(left, top, right, bottom);
            canvas.drawRect(drawableRect, boxPaint);

            // 创建显示在检测对象旁边的文本
            String drawableText = result.getLabel() + " " + String.format("%.2f", result.getScore());

            // 绘制显示文本背后的矩形
            textBackgroundPaint.getTextBounds(drawableText, 0, drawableText.length(), bounds);
            float textWidth = bounds.width();
            float textHeight = bounds.height();
            canvas.drawRect(left, top, left + textWidth + BOUNDING_RECT_TEXT_PADDING, top + textHeight + BOUNDING_RECT_TEXT_PADDING, textBackgroundPaint);

            // 绘制检测对象的文本
            canvas.drawText(drawableText, left, top + bounds.height(), textPaint);
        }
    }

    // 设置检测结果
    public void setResults(List<DetectItem> detectionResults, int imageHeight, int imageWidth) {
        results = detectionResults;

        // PreviewView 处于 FILL_START 模式。因此，我们需要放大边界框以匹配
        // 捕获的图像将显示的大小。
        scaleFactor = Math.max(getWidth() * 1f / imageWidth, getHeight() * 1f / imageHeight);
    }

    // 边界矩形文本填充的间距
    private static final int BOUNDING_RECT_TEXT_PADDING = 8;
}
